<template>
  <div>
    <el-table
      border
      style="width: 100%"
      :data="assignList"
      row-key="id"
      :expand-row-keys="defaultRow"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
    >
      <el-table-column label="名称" prop="name">
        <template> </template>
      </el-table-column>
      <el-table-column label="权限值" prop="code"> </el-table-column>
      <el-table-column label="跳转权限值" prop="toCode"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-plus"
            :disabled="row.level === 4"
            :title="getaddtitle(row.level)"
            @click="addAssign(row)"
          ></el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            :disabled="row.level === 1"
            :title="row.level === 4 ? '修改功能' : '修改菜单'"
            @click="edit(row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            :disabled="row.level === 1"
            title="删除"
            @click="remove(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form
        :model="permission"
        ref="permission"
        :rules="permissionRules"
        label-width="120px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="permission.name"></el-input>
        </el-form-item>
        <el-form-item label="功能权限值" prop="code">
          <el-input v-model="permission.code"></el-input>
        </el-form-item>
        <el-form-item label="跳转权限值" v-show="permission.id">
          <el-input v-model="permission.toCode"></el-input>
        </el-form-item>
        <el-form-item label-width="85%">
          <el-button size="small" @click="resetData">取消</el-button>
          <el-button type="primary" size="small" @click="save">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
const menurules = {
  name: [{ required: true, message: "权限名称为必填项" }],
  code: [{ required: true, message: "权限值为必填项" }],
};
const btnrules = {
  name: [{ required: true, message: "按钮名称为必填项" }],
  code: [{ required: true, message: "按钮权限值为必填项" }],
};
export default {
  name: "Permission",
  data() {
    return {
      assignList: [],
      defaultRow: [],
      loading: false,
      dialogVisible: false,
      permission: {
        // 要操作的菜单权限对象
        level: 0,
        name: "",
        code: "",
        toCode: "",
      },
    };
  },
  mounted() {
    this.getAssignList();
  },
  computed: {
    dialogTitle() {
      const { level, id } = this.permission;
      if (id) {
        return level === 4 ? "修改功能" : "修改菜单";
      } else {
        return level === 4 ? "添加功能" : "添加菜单";
      }
    },
    permissionRules() {
      const { level } = this.permission;
      return level === 4 ? btnrules : menurules;
    },
  },
  methods: {
    //获取所有的按钮权限数据
    async getAssignList() {
      this.loading = true;
      const result = await this.$API.permission.getPermissionList();
      this.assignList = result.data.children;
      this.defaultRow = [result.data.children[0].id];
      this.loading = false;
    },
    //按钮添加title
    getaddtitle(level) {
      if (level === 1 || level === 2) {
        return "添加菜单";
      } else if (level === 3) {
        return "添加功能";
      }
    },
    //添加一个权限
    addAssign(row) {
      this.dialogVisible = true;
      this.permission.pid = row.id;
      this.permission.level = row.level + 1;
      this.permission.type = this.permission.level === 4 ? 2 : 1;
      this.$nextTick(() => {
        this.$refs.permission.clearValidate();
      });
    },
    //保存按钮
    save() {
      const { id } = this.permission;
      this.$refs.permission.validate(async (v) => {
        if (v) {
          if (id) {
            await this.$API.permission.updatePermission(this.permission);
          } else {
            await this.$API.permission.addPermission(this.permission);
          }
          this.$message.success(id?'修改成功':'添加成功')
          this.resetData();
          this.getAssignList();
        }
      });
    },
    //取消按钮回调
    resetData() {
      this.dialogVisible = false;
      this.permission = {
        level: 0,
        name: "",
        code: "",
        toCode: "",
      };
    },
    //删除某个权限
    async remove(id) {
      try {
        await this.$API.permission.removePermission(id);
        this.$message.success("删除成功");
        this.getAssignList();
      } catch (error) {}
    },
    //修改按钮
    edit(row) {
      this.permission = { ...row };
      this.dialogVisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
</style>